﻿@using Newtouch.Tools;
@using Newtouch.Infrastructure;
@model Newtouch.HIS.Domain.DTO.OutputDto.HosSettPatClassifyChargePreviewDto
@{
    ViewBag.Title = "SettPreview";
    Layout = "~/Views/Shared/_Form.cshtml";
}

<style type="text/css">
    .table {
        margin-bottom: 1%;
    }

        .table > tbody > tr > td, .table > tbody > tr > th {
            font-weight: normal;
            border: 0;
            line-height: 1.0;
            padding:5px 1px;
        }
</style>

<div style="margin:2% 7% 0 7%;">
    <table class="table">
        <tr>
            <th class="text-right">住院号：</th>
            <td class="text-left">
                @Model.HospSettPatInfo.zyh
            </td>
            <th class="text-right">卡号：</th>
            <td class="text-left">
                @Model.HospSettPatInfo.kh
            </td>
            <th class="text-right">姓名：</th>
            <td class="text-left">
                @Model.HospSettPatInfo.xm
            </td>
            <th class="text-right">性别：</th>
            <td class="text-left">@(Model.HospSettPatInfo.xb == null ? "" : ((EnumSex)(Model.HospSettPatInfo.xb.ToInt(-1))).GetDescription(""))</td>
        </tr>
        <tr>
            <th class="text-right">年龄：</th>
            <td class="text-left">
                @Model.HospSettPatInfo.csny.ToAge()
            </td>
            <th class="text-right">记账：</th>
            <td class="text-left">
                @Model.jzfy.ToString("0.00")
            </td>
            <th class="text-right">分类自负：</th>
            <td class="text-left">
                @Model.flzf.ToString("0.00")
            </td>
            <th class="text-right">自负：</th>
            <td class="text-left">
                @Model.zifu.ToString("0.00")
            </td>
        </tr>
        <tr>
            <th class="text-right"></th>
            <td class="text-left"></td>
            <th class="text-right"></th>
            <td class="text-left"></td>
            <th class="text-right">自费：</th>
            <td class="text-left">
                @Model.zifei.ToString("0.00")
            </td>
            <th class="text-right">其他：</th>
            <td class="text-left">
                @Model.jmje.ToString("0.00")
            </td>
        </tr>
    </table>
</div>

<div class="rows" style="margin:0 3%;margin-top: 0.5%;">
    <div class="panel panel-default" style="margin-bottom:0;">
        <div class="panel-heading">
            费用结算
        </div>
        <div class="panel-body" style="padding: 5px 15px;">
            <div>
                @*款项操作*@
                <table class="form">
                    <tr>
                        <td class="formValue">
                            <select id="ddlZYJSChargeType" class="form-control required"></select>
                        </td>
                        <td class="formValue" style="width:100px;">
                            <input type="text" id="txtszje" class="form-control form-control-focus" />
                        </td>
                        <td class="formValue">
                            &nbsp;
                            <i class="fa fa-plus-circle fa-lg" id="btn_Add" style="cursor:pointer;" onclick="btn_add()"></i>
                            &nbsp;&nbsp;
                            <i class="fa fa-minus-circle fa-lg" id="btn_Remove" style="cursor:pointer;" onclick="btn_delete()"></i>
                        </td>
                        <td class="formValue">
                            帐户余额：@Model.zhye
                        </td>
                    </tr>
                </table>
                <table id="tbZhszGridList"></table>
            </div>
            <div style="margin-top:1%;">
                <table class="table" style="margin-left:2%;font-size:14px;">
                    <tr>
                        <th class="text-right"></th>
                        <td class="text-left"></td>
                        <th class="text-right"></th>
                        <td class="text-left"></td>
                        <th class="text-right" style="font-weight:700;">应收款：</th>
                        <td class="text-left" id="tdYsk">
                            <span>@Model.yingshoukuan.ToString("0.00")</span>
                        </td>
                    </tr>
                    <tr>
                        <th class="text-right" style="font-weight:700;color:red;">找零：</th>
                        <td class="text-left" id="tdZhaoling" style="color:red;">
                            <span>@Model.zhaoling.ToString("0.00")</span>
                        </td>
                        <th class="text-right">舍入差额：</th>
                        <td class="text-left">
                            @Model.srce.ToString("0.00")
                        </td>
                        <th class="text-right" style="font-weight:700;">实收款：</th>
                        <td class="text-left" id="tdSsk">
                            <span>@Model.ssk.ToString("0.00")</span>
                        </td>
                    </tr>
                </table>
            </div>
            <div style="margin-top:1%;">
                <table>
                    <tr class="form">
                        <td class="formValue">
                            <input type="text" class="form-control" id="txtfph" readonly="readonly" disabled value="@Model.fph" placeholder="请选择发票号" />
                        </td>
                        <td class="formValue">
                            <input type="button" class="btn btn-default btn-md" value="选发票号 " onclick="ShowInvoicePanel()" />
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    //患者交款记录 jGrid声明
    $('#tbZhszGridList').newtouchLocalDataGrid({
        height: 80,
        unwritten: false,
        sortable: false,
        colModel: [
            { label: '支付方式代码', name: 'zffs', hidden: true },
            { label: '支付方式', name: 'zffsmc', width: 100, align: 'left' },
            { label: '支付金额', name: 'zfje', width: 100, align: 'left' },
            { label: '支付账号', name: 'zfzh', width: 200, align: 'left' }
        ]
    });

    var yjjzhzhxz = '@Model.HospSettPatInfo.zyyjjzhzhxz';
    var yjjzhzffs = "";//对应的xjzffs
    var yjjzhzffsmc = "";//对应的xjzffs

    $("#ddlZYJSChargeType").newtouchBindSelect({
        id: "xjzffs",
        text: "xjzffsmc",
        datasource: function () {
            //遍历数据源
            var resultObjArr = new Array();
            if(top.clients && top.clients.SysForCashPayList){
                $.each(top.clients.SysForCashPayList, function (idx, val) {
                    if (val.zhxz == yjjzhzhxz || val.zhxz == 'a') {
                        if (val.zhxz == yjjzhzhxz) {
                            yjjzhzffs = val.xjzffs;
                            yjjzhzffsmc = val.xjzffsmc;
                        }
                        resultObjArr.push(val);
                    }
                });
            }
            return resultObjArr;
        }
    });

    $(function () {
        if (!yjjzhzhxz || !yjjzhzffs || !yjjzhzffsmc || $('#ddlZYJSChargeType option[value=' + yjjzhzffs + ']').length == 0) {
            throw new Error('zhxz error');
        }

        $("#ddlZYJSChargeType").val($('#ddlZYJSChargeType option:first').attr('value'));
        $('#ddlZYJSChargeType option[value=' + yjjzhzffs + ']').attr('disabled', 'disabled').trigger("change");

        $('#tbZhszGridList').newtouchLocalDataGrid(null, [{ zffs: yjjzhzffs, zffsmc: yjjzhzffsmc, zfje: '@(Model.zhye.ToString("0.00"))', zfzh: "@((Model.HospSettPatInfo.zyyjjzh ?? 0) > 0 ? Model.HospSettPatInfo.zyyjjzh.Value.ToString() : "")" }]);
    });

    function btn_add() {
        var chargeType = $("#ddlZYJSChargeType").val();
        var chargeTypeText = $("#ddlZYJSChargeType option:selected").text();
        var szje = $("#txtszje").val();
        $("#txtszje").val('');  //
        if (szje) {
            szje = parseFloat(szje).toFixed(2);
            if (szje == 0 || isNaN(szje) || szje > 200000) {
                return;
            }
        }
        if (chargeType && chargeTypeText && szje) {
            //一种支付方式 重复添加判断
            if (chargeType == yjjzhzffs) {
                return;
            }
            $('#tbZhszGridList').newtouchLocalDataGrid(null, [{ zffs: chargeType, zffsmc: chargeTypeText, zfje: szje, zfzh: "" }]);
        }
        reCalcZhaoling();
    };

    function btn_delete() {
        var rowid = jQuery("#tbZhszGridList").jqGrid("getGridParam", "selrow");
        if ($("#tbZhszGridList").jqGrid('getRowData', rowid)["zffs"] == yjjzhzffs) {
            return; //预交金账户 记录不能删
        }
        if (rowid) {
            $('#tbZhszGridList').jqGrid("delRowData", rowid);
        }
        reCalcZhaoling();
    };

    //重新计算找零
    function reCalcZhaoling() {
        var ysk = $.trim($("#tdYsk span").text());
        var ssk = 0;
        var rowIds = $("#tbZhszGridList").getDataIDs();
        $.each(rowIds, function (idx, val) {
            var zfjestr = $("#tbZhszGridList").jqGrid('getRowData', val)["zfje"];
            if (zfjestr) {
                var zfje = parseFloat(parseFloat(zfjestr).toFixed(2));
                ssk += zfje;
            }
        });
        $('#tdSsk span').html(ssk.toFixed(2));
        $('#tdZhaoling span').html((ssk - ysk).toFixed(2));
    }

    $('#txtszje').keydown(function (event) {
        event = event || window.event;
        if (event.keyCode == 13) {
            if ($('#btn_Add:visible').length == 1) {
                btn_add();
            }
        }
    });

    //弹出发票号的窗口
    function ShowInvoicePanel() {
        $.modalOpen({
            id: "InvoiceNoChoosePanel",
            title: "选发票号",
            url: "/OutpatientManage/OutpatientReg/ChooseInvoice?from=FormShowSett",
            width: "300px",
            height: "200px",
            callBack: function (iframeId) {
                top.InvoiceNoChoosePanel.checkFPH();    //窗口点确定的回调函数
            }
        });
    }

    //提交结算
    function submitSett() {
        var postData = {};
        postData['zyh'] = '@(Model.zyh)';
        postData['fph'] = $('#txtfph').val(); //'@(Model.fph)';
        postData['expectedcyrq'] = '@(Model.expectedcyrq)';  //
        postData['expectedyjjzhye'] = '@(Model.zhye)';  //
        //期望的结算金额 防止过程中变更
        postData['expectedjsje'] = @(Model.expectedjsje);
        //期望的找零 防止过程中变更
        postData['expectedzhaoling'] = $('#tdZhaoling span').text();
        if($('#tdZhaoling span').text() && parseFloat($.trim($('#tdZhaoling span').text())) < 0){
            $.modalAlert("找零不能为负", 'error');
            return;
        }
        //期望的结算金额 防止过程中变更
        postData['shishoukuan'] = $('#tdSsk span').text();
        //支付方式 json串
        var xjzfArr = new Array();
        var rowIds = $("#tbZhszGridList").getDataIDs();
        $.each(rowIds, function (idx, val) {
            var thisRowData = $("#tbZhszGridList").jqGrid('getRowData', val);
            xjzfArr.push({xjzffs : thisRowData['zffs'], zfje : parseFloat(thisRowData['zfje']), zfzh : parseInt(thisRowData['zfzh'])});
        });
        postData['xjzfListStr'] = JSON.stringify(xjzfArr);

        $.submitForm({
            url: "/HospitalizationManage/Settlement/SubmitSett",
            param: postData,
            successwithtipmsg: false,
            success: function (data) {
                //弹另外一个层 结算结果 方法定义在Index中
                $.currentWindow().window.showSettSuccessDialog(data.data.yingshoukuan, data.data.ssk, data.data.srce, data.data.zhaoling);
                $.currentWindow().window.newtouch_globalevent_f4();
            }
        })
    }

</script>